﻿using Microsoft.EntityFrameworkCore;
using YDT_SeckillService.Domain.Entities;
using YDT_SeckillService.Domain.Enums;
using YDT_SeckillService.Domain.Interfaces;
using YDT_SeckillService.Infrastructure.Data;

namespace YDT_SeckillService.Infrastructure.Repositorys
{
    /// <summary>
    /// 秒杀商品仓储实现 - 实现秒杀商品数据访问
    /// </summary>
    public class SeckillProductRepository : ISeckillProductRepository
    {
        private readonly SeckillServiceDbContext _context;

        /// <summary>
        /// 构造函数
        /// </summary>
        public SeckillProductRepository(SeckillServiceDbContext context)
        {
            _context = context;
        }

        /// <summary>
        /// 根据ID获取秒杀商品
        /// </summary>
        public async Task<SeckillProduct?> GetByIdAsync(Guid id)
        {
            return await _context.SeckillProducts
                .FirstOrDefaultAsync(sp => sp.Id == id);
        }

        /// <summary>
        /// 根据商品ID获取秒杀商品
        /// </summary>
        public async Task<SeckillProduct?> GetByProductIdAsync(Guid productId)
        {
            return await _context.SeckillProducts
                .FirstOrDefaultAsync(sp => sp.ProductId == productId);
        }

        /// <summary>
        /// 根据秒杀活动ID获取秒杀商品
        /// </summary>
        public async Task<IEnumerable<SeckillProduct>> GetBySeckillIdAsync(Guid seckillId)
        {
            return await _context.SeckillProducts
                .Where(sp => sp.TimeId == seckillId)
                .OrderByDescending(sp => sp.SeckillIsTop)
                .ThenBy(sp => sp.CreateTime)
                .AsNoTracking() // 只读查询，不跟踪变更
                .ToListAsync();
        }

        /// <summary>
        /// 获取所有秒杀商品
        /// </summary>
        public async Task<IEnumerable<SeckillProduct>> GetAllAsync()
        {
            return await _context.SeckillProducts
                .OrderByDescending(sp => sp.CreateTime)
                .AsNoTracking() // 只读查询，不跟踪变更
                .ToListAsync();
        }

        /// <summary>
        /// 获取热门秒杀商品
        /// </summary>
        public async Task<IEnumerable<SeckillProduct>> GetHotSeckillProductsAsync(int count)
        {
            return await _context.SeckillProducts
                .Where(sp => sp.SeckillStatus == SeckillProductStatus.Active && sp.SeckillStock > 0)
                .OrderByDescending(sp => sp.SeckillIsTop)
                .ThenByDescending(sp => sp.CreateTime)
                .Take(count)
                .AsNoTracking() // 只读查询，不跟踪变更
                .ToListAsync();
        }

        /// <summary>
        /// 添加秒杀商品
        /// </summary>
        public async Task AddAsync(SeckillProduct seckillProduct)
        {
            await _context.SeckillProducts.AddAsync(seckillProduct);
            await _context.SaveChangesAsync();
        }

        /// <summary>
        /// 更新秒杀商品
        /// </summary>
        public async Task UpdateAsync(SeckillProduct seckillProduct)
        {
            _context.SeckillProducts.Update(seckillProduct);
            await _context.SaveChangesAsync();
        }

        /// <summary>
        /// 删除秒杀商品
        /// </summary>
        public async Task DeleteAsync(Guid id)
        {
            var seckillProduct = await _context.SeckillProducts.FindAsync(id);
            if (seckillProduct != null)
            {
                _context.SeckillProducts.Remove(seckillProduct);
                await _context.SaveChangesAsync();
            }
        }

        /// <summary>
        /// 检查秒杀商品是否存在
        /// </summary>
        public async Task<bool> ExistsAsync(Guid id)
        {
            return await _context.SeckillProducts.AnyAsync(sp => sp.Id == id);
        }
    }
}
